package org.iteam.core.service.oracle.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.core.service.CenterService;
import org.iteam.core.view.TreeNode;
import org.iteam.core.view.sys.SysFunctionView;

public class CenterServiceImpl extends CenterService {

	public List<TreeNode> getMenus(Connection conn, String role, String parent) {
		StringBuffer sbStr = new StringBuffer("SELECT  (");
		sbStr.append("  SELECT COUNT(DISTINCT code) FROM sys_function sf2  ");
		sbStr.append("  where sf2.status = 'ENABLE' AND  sf2.TYPE = 'MENU' AND sf2.parent = T1.code ");
		sbStr.append(" )  AS MENU_COUNT,  (  ");
		sbStr.append("  SELECT COUNT(DISTINCT CODE)  FROM  (");
		sbStr.append(" SELECT  DISTINCT sf1.CODE,TO_CHAR(SP1.PERMISSION_ROLE) PERMISSION_ROLE,sf1.PARENT FROM sys_function sf1 ");
		sbStr.append(" INNER JOIN sys_permission sp1 ON sf1.code = sp1.PERMISSION_FUNCTION");
		sbStr.append("  INNER JOIN sys_role sr1 ON sp1.PERMISSION_ROLE = sr1.CODE");
		sbStr.append("  where sp1.\"MODE\" = 'PERMIT'  AND sf1.TYPE = 'MODULE' AND sf1.\"MODE\" = 'AUTH'");
		sbStr.append(" AND sf1.STATUS = 'ENABLE' AND sr1.STATUS = 'ENABLE' and sp1. PERMISSION_ROLE = ? ");
		sbStr.append(" UNION ");
		sbStr.append(" SELECT  CODE, ? as PERMISSION_ROLE,PARENT FROM sys_function sf2 ");
		sbStr.append(" WHERE sf2.\"MODE\" IN ('LOGIN','NOAUTH') AND sf2.STATUS ='ENABLE' AND sf2.TYPE = 'MODULE' ");
		sbStr.append(" ) TEMP_1 WHERE TEMP_1.PARENT = T1.code");
		sbStr.append(" )  AS  MODULE_COUNT,   ");
		List<Object> params = new ArrayList<Object>();
		params.add(role);
		params.add(role);
		if (StringUtils.isEmpty(parent)) {
			sbStr.append(" T1.* FROM sys_function T1 WHERE T1.STATUS = 'ENABLE' AND T1.PARENT IS NULL AND T1.TYPE = 'MENU' ");
		} else {
			sbStr.append(" T1.* FROM sys_function T1 WHERE T1.STATUS = 'ENABLE' AND T1.PARENT = ? AND T1.TYPE = 'MENU' ");
			params.add(parent);
		}
		sbStr.append(" ORDER BY T1.SORT DESC,T1.CREATE_TIME DESC ");
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		try {
			List<SysFunctionView> lists = super.query(conn,
					SysFunctionView.class, sbStr.toString().toUpperCase(),
					params.toArray());
			for (SysFunctionView sfv : lists) {
				if (sfv.getMenu_count() > 0 || sfv.getModule_count() > 0) {
					TreeNode node = new TreeNode(sfv.getCode(), sfv.getName(),
							sfv.getUrl());
					node.setSort(sfv.getSort());
					if (sfv.getMenu_count() > 0) {
						node.getChildren().addAll(
								getMenus(conn, role, sfv.getCode()));
					}
					if (sfv.getModule_count() > 0) {
						node.getChildren().addAll(
								getModules(conn, role, sfv.getCode()));
					}
					node.setChildren(sort(node.getChildren()));
					if (node.getChildren() != null && node.getChildren().size() > 0)
						nodes.add(node);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nodes;

	}

	public List<TreeNode> getMenus(String role) {
		Connection conn = null;
		try {
			conn = super.getConnection();
			return getMenus(conn, role, null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				super.close(conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;

	}

	public List<TreeNode> getModules(Connection conn, String role, String parent)
			throws SQLException {
		StringBuffer sbStr = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		sbStr.append("  SELECT  DISTINCT * FROM  (");
		sbStr.append(" SELECT  DISTINCT SF1.CODE,	SF1.URL,	SF1.NAME	,SF1.SORT	,SF1.CREATE_TIME,TO_CHAR(SP1.PERMISSION_ROLE) PERMISSION_ROLE,SF1.PARENT  FROM sys_function sf1 ");
		sbStr.append(" INNER JOIN sys_permission sp1 ON sf1.code = sp1.PERMISSION_FUNCTION");
		sbStr.append("  INNER JOIN sys_role sr1 ON sp1.PERMISSION_ROLE = sr1.CODE");
		sbStr.append("  where sp1.\"MODE\" = 'PERMIT'  AND sf1.TYPE = 'MODULE' AND sf1.\"MODE\" = 'AUTH'");
		sbStr.append(" AND sf1.STATUS = 'ENABLE' AND sr1.STATUS = 'ENABLE' and sp1. PERMISSION_ROLE = ? ");
		sbStr.append(" UNION ");
		sbStr.append(" SELECT  CODE ,URL,NAME,SORT,CREATE_TIME, ? as PERMISSION_ROLE,PARENT FROM sys_function sf2 ");
		sbStr.append(" WHERE sf2.\"MODE\" IN ('LOGIN','NOAUTH') AND sf2.STATUS ='ENABLE' AND sf2.TYPE = 'MODULE' ");
		sbStr.append(" ) TEMP_1 WHERE TEMP_1.PARENT = ? ORDER BY TEMP_1.SORT DESC,TEMP_1.CREATE_TIME DESC ");
		params.add(role);
		params.add(role);
		params.add(parent);
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		List<SysFunctionView> lists = super.query(conn, SysFunctionView.class,
				sbStr.toString().toUpperCase(), params.toArray());
		for (SysFunctionView sfv : lists) {
			TreeNode node = new TreeNode(sfv.getCode(), sfv.getName(),
					sfv.getUrl());
			node.setSort(sfv.getSort());
			nodes.add(node);
		}
		return nodes;

	}

	public List<TreeNode> getMenusByRole(String role) throws SQLException {
		return getMenus(role);
	}
}
